package cn.sunshine.system.service.impl;

import cn.sunshine.basic.service.impl.BaseServiceImpl;
import cn.sunshine.system.domain.Menu;
import cn.sunshine.system.mapper.MenuMapper;
import cn.sunshine.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Jiang
 * @since 2022-07-12
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
@Autowired
private MenuMapper menuMapper;
    @Override
    public List<Menu> findMenuTree() {
        // 查询菜单所有的数据集合
        List<Menu> menus = menuMapper.loadAll();
        // 创建一个集合用来装部门数据
        List<Menu> arrayList = new ArrayList<>();

        // 将集合转换成map集合
        //Map<Long, Menu> map = arrayList.stream().collect(Collectors.toMap(Menu::getId, item -> item));
        HashMap<Long, Menu> map = new HashMap<>();
        for (Menu menu : menus) {
            map.put(menu.getId(), menu);
        }
        for (Menu first : menus) {
            if (first.getParentId() == null) {
                arrayList.add(first);
            } else {
                Menu menu = map.get(first.getParentId());
                if (menu != null) {
                    menu.getChildren().add(first);
                }
            }
        }

        return arrayList;
    }
}
